package com.yusong.algorithm.stack;

import java.util.Deque;
import java.util.LinkedList;
import java.util.List;

/**
 * 简化路径
 */
public class SimplifyPath71 {



    private static final String CURRENT_PATH = ".";
    private static final String PARENT_PATH = "..";

    public String simplifyPath(String path) {

        String[] strs = path.split("/");
        Deque<String> stack = new LinkedList<>();
        for(String str :strs){
            if(null == str || "".equals(str) || CURRENT_PATH.equals(str)){
                continue;
            }else if (PARENT_PATH.equals(str)){
                stack.pollFirst();
            }else {
                stack.push(str);
            }
        }
        StringBuffer sb = new StringBuffer();
        while (stack.size() > 0){
            sb.append("/"+stack.pollLast());
        }
        if(sb.length() == 0){
            return "/";
        }

        LinkedList<Integer> res = new LinkedList<>();
        return sb.toString();
    }
}
